﻿import QtQuick 2.4
import QtQuick.Window 2.3
import QtQuick.Controls 2.4
import QtQuick.Extras 1.4
import QtQuick.Controls.Styles 1.4
import QtQuick.Layouts 1.3
import "components"
import timer 1.0
import QtCharts 2.3

Item {
    id: root
    width: 256
    height: width
    property int devIndex: 0
    property int cpuRate: 0         // 0~1000       -> 0~100%
    property int memRate: 0         // 0~1000       -> 0~100%
    property int tempera: 30        // -1000~1000   -> -100℃~100℃
    property int videoTemp: 0       // 显卡温度 -1000~1000   -> -100℃~100℃
    property int hddTemp: 0         // 硬盘温度 -1000~1000   -> -100℃~100℃
    property int linesMask: 0x07     // 曲线-显示频闭
    property string macString: ""
    property string ipString: ""
    property string name: ""
    property string testcaseStr: "等待连接"
    property int    statusLevel:  0
    property string statusString: ""
    property int testTimes: 0       // 测试时间，单位：秒
    property int lineIndexs: 0
    property int cmdTimes: 0        // 当前命令测试时间，单位：秒
    property int cmdProcess: 0      // 当前命令测试进度：0~1000 -> 0~100%
    property int testProcess: 0     // 总体测试进度，0~1000 -> 0~100%
    property int statusRefresh: 0   // 刷新指示灯控制

    Timer {
        id: ledTimer
        interval: 2000
        onTimeout:{
            roundButton_status_bg.color = "red"
        }
    }

    onTestTimesChanged: {
        label_time.text = " " + "时间：" + cmdTimes + "s / "  + (testTimes/60).toFixed(0) + "分" + testTimes%60 + "秒"
    }

    // 刷新设备指示灯
    onStatusRefreshChanged: {
        if(statusLevel <= 1){
            ledTimer.start()
            statusColorAnimation_normal.start()
        }else if(statusLevel === 2){
            ledTimer.stop()
            statusColorAnimation_warn.start()
        }else{
            ledTimer.stop()
            statusColorAnimation_err.start()
        }

        if(lineIndexs < 1){
            frame_chart.series(0).axisY.max = 100;
            frame_chart.series(0).axisX.max = 300;
        }
        if(lineIndexs > 300){
            frame_line_cpu.removePoints(0,1)
            frame_line_mem.removePoints(0,1)
            frame_line_temp.remove(0,1)
            frame_line_vdoTemp.remove(0,1)
            frame_line_hddTemp.remove(0,1)
            frame_chart.series(0).axisX.max = lineIndexs;
            frame_chart.series(0).axisX.min = lineIndexs-300;
        }

        frame_line_cpu.append(lineIndexs, cpuRate*0.1)
        frame_line_cpu.name = "CPU:" + (cpuRate*0.1).toFixed(0) + "%"
        frame_line_mem.append(lineIndexs, memRate*0.1)
        frame_line_mem.name = "内存:" + (memRate*0.1).toFixed(0) + "%"
        frame_line_temp.append(lineIndexs, tempera*0.1)
        frame_line_vdoTemp.name = "显卡:" + (videoTemp*0.1).toFixed(0) + "℃"
        frame_line_vdoTemp.append(lineIndexs, videoTemp*0.1)
        frame_line_hddTemp.name = "硬盘:" + (hddTemp*0.1).toFixed(0) + "℃"
        frame_line_hddTemp.append(lineIndexs, hddTemp*0.1)

        if(tempera*0.1 > 60){
            frame_line_temp.name = "温度:" + (tempera*0.1).toFixed(0) + "℃(温度过高)"
            lineIndexs = lineIndexs + 1
            frame_line_temp.color = "red"
        }else{
            frame_line_temp.name = "温度:" + (tempera*0.1).toFixed(0) + "℃"
            lineIndexs = lineIndexs + 1
            frame_line_temp.color = "orange"
        }
    }

    ChartView {
        //padding: 0
        id: frame_chart
        x: 0
        y: 0
        width: root.width
        height: root.height * 0.5
        //theme: ChartView.ChartThemeBrownSand
        antialiasing: true
        //animationOptions: ChartView.GridAxisAnimations
        legend.font.pointSize: parent.height / 40

        ValueAxis{
            id: yAxis
            min: 0
            max: 100
        }

        LineSeries {
            id: frame_line_cpu
            name: "CPU"
            color: "blue"
            width: 2
            visible: linesMask&0x01
        }
        LineSeries {
            id: frame_line_mem
            name: "内存"
            color: "limegreen"
            width: 2
            visible: linesMask&0x02
        }
        LineSeries {
            id: frame_line_temp
            name: "温度"
            color: "orange"
            width: 2
            visible: linesMask&0x04
        }
        LineSeries {
            id: frame_line_vdoTemp
            name: "显卡"
            color: "magenta"
            width: 2
            visible: linesMask&0x08
        }
        LineSeries {
            id: frame_line_hddTemp
            name: "硬盘"
            color: "darkslategrey"
            width: 2
            visible: linesMask&0x10
        }
    }

    Rectangle {
        id: frame_detail
        x: 0
        y: frame_chart.height
        width: root.width
        height: root.height - frame_chart.height

        ProgressBar {
            id: progressbar_test
            value: cmdProcess
            to: 1000
            width: parent.width

            contentItem: Rectangle{
                width: progressbar_test.visualPosition * progressbar_test.width
                color: cmdProcess > 1000 ? "red" : "black"
            }
        }

        Label {
            id: textMAC
            color: root.enabled?"blue":"white"
            x: 0
            anchors.top: progressbar_test.bottom
            anchors.topMargin: parent.height / 28
            width: parent.width * 0.5
            height: parent.height * 0.14
            font.pointSize: parent.height / 16
            verticalAlignment: Text.AlignVCenter
            text: " " + macString
            background: Rectangle{
                width: parent.width
                height: parent.height
                color: "#aaaaaa"
            }
        }

        Label {
            id: textIP
            color: root.enabled?"black":"white"
            x: 0
            anchors.top: textMAC.bottom
            anchors.topMargin: parent.height * 0.01
            width: parent.width * 0.5
            height: parent.height * 0.14
            font.pointSize: parent.height / 17
            verticalAlignment: Text.AlignVCenter
            text: " " + ipString
            //readOnly: true
            background: Rectangle{
                width: parent.width
                height: parent.height
                color: "#aaaaaa"
            }
        }

        Label {
            id: label_proress
            anchors.left: textMAC.right
            anchors.leftMargin: parent.height * 0.01
            anchors.verticalCenter: textMAC.verticalCenter
            font.pointSize: parent.height / 20
            width: parent.width - x
            height: parent.height * 0.14
            text: " 进度：" + (cmdProcess*0.1).toFixed(1) + "%" + " / "  + (testProcess*0.1).toFixed(1) + "%"
            verticalAlignment: Text.AlignVCenter
            //readOnly: true
            background: Rectangle{
                width: parent.width
                height: parent.height
                color: "#aaaaaa"
            }
        }

        Label {
            id: label_time
            anchors.verticalCenter: textIP.verticalCenter
            anchors.left: textIP.right
            anchors.leftMargin: parent.height * 0.01
            width: parent.width - x
            height: parent.height * 0.14
            font.pointSize: parent.height / 20
            text: " " + "时间：" + 0 + "s / "  + 0 + "分" + 0 + "秒"
            // text: " " + "时间：" + cmdTimes + "s / "  + (testTimes/60).toFixed(0) + "分" + testTimes%60 + "秒"
            verticalAlignment: Text.AlignVCenter
            //readOnly: true
            background: Rectangle{
                width: parent.width
                height: parent.height
                color: "#aaaaaa"
            }
        }

        ScrollView {
            id: testDetailScrollView
            x: 0
            anchors.top: textIP.bottom
            width: parent.width
            height: parent.height * 0.46
            anchors.topMargin: 2

            TextArea {
                id: testDetailField
                x: 0
                background: Rectangle{
                    width: parent.width
                    height: parent.height
                    color: "#aaaaaa"
                }
                readOnly: true
                width: parent.width
                height: parent.height
                font.pointSize: frame_detail.height / 16
                text: testcaseStr
                textFormat: Qt.RichText
            }
        }

        ColorAnimation {
            id: statusColorAnimation_normal
            duration: 800
            properties: "color"
            target: roundButton_status_bg
            from: "#bbbbbb"
            to: "green"
        }
        ColorAnimation {
            id: statusColorAnimation_warn
            duration: 800
            properties: "color"
            target: roundButton_status_bg
            from: "#bbbbbb"
            to: "yellow"
        }
        ColorAnimation {
            id: statusColorAnimation_err
            duration: 800
            properties: "color"
            target: roundButton_status_bg
            from: "#bbbbbb"
            to: "red"
        }


        RoundButton {
            id: roundButton_status
            font.pointSize: parent.height / 18
            anchors.bottom: parent.bottom
            x: parent.width * 0.8
            width: parent.width * 0.06
            height: width
            text: "S"
            background: Rectangle{
                id: roundButton_status_bg
                radius: roundButton_status.width
                color: "#bbbbbb"
            }
        }

        RoundButton {
            id: roundButton_run
            anchors.bottom: parent.bottom
            font.pointSize: parent.height / 18
            x: parent.width * 0.87
            width: parent.width * 0.06
            height: width
            text: "R"

            onClicked: {
                systimer.testDevControl(devIndex);
            }
        }

        RoundButton {
            id: roundButton_set
            anchors.bottom: parent.bottom
            font.pointSize: parent.height / 18
            x: parent.width * 0.94
            width: parent.width * 0.06
            height: width
            text: "M"

            onClicked: {
                systimer.testDevSetup(devIndex);
            }
        }

        Text {
            id: status
            x: 0
            color: (statusLevel==0?"black":(statusLevel==1?"green":(statusLevel==2?"orange":"red")))
            anchors.bottom: parent.bottom
            text: " " + statusString
            font.bold: true
            verticalAlignment: Text.AlignVCenter
            height: roundButton_status.height
            font.pixelSize: parent.height / 17
        }
    }
}
